package com.smart.controller.sys;

import java.util.Map;
import java.util.List;
import java.util.HashMap;
import java.util.ArrayList;
import com.smart.util.Page;
import com.smart.util.Pageable;
import com.smart.util.DateUtil;
import com.smart.util.ExportExcelUtils;
import com.smart.util.Messages;
import javax.annotation.Resource;
import io.swagger.annotations.Api;
import com.smart.entity.SysMainRecords;
import com.smart.controller.BaseController;
import io.swagger.annotations.ApiOperation;
import com.smart.service.MainRecordsService;
import com.smart.vo.sys.SysMainRecordsExcelVo;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author lgb
 * @date 2020年6月11日 下午4:50:00
 * Copyright (C) 深圳工勘科技
 * @说明: 保养记录管理
 **/
@RequestMapping("/sys/mRecords")
@Api(value="attendance",tags="保养记录管理")
@RestController("sysMainRecordsController")
public class MainRecordsController extends BaseController{

	@Resource
	private MainRecordsService mainRecordsService;
	
	/**
	 * @param pageable
	 * @return 分页查询数据
	 **/
	@GetMapping(value="/page")
	@ApiOperation(value="Sys=>获取保养记录列表",notes="Sys获取保养记录列表")
	public Messages<Page<SysMainRecords>> page(String driverName,
			String carNo,String createDate_start,String createDate_end,Pageable pageable){
		Map<String,String> search = new HashMap<>();
		search.put("driverName", driverName);
		search.put("carNo", carNo);
		search.put("createDate_start", createDate_start);
		search.put("createDate_end", createDate_end);
		pageable.setSearch(search);
		return Messages.success(mainRecordsService.findPage(pageable));
	}
	
	@GetMapping(value="/detail")
	@ApiOperation(value="Sys=>获取保养记录详情",notes="Sys获取保养记录详情")
	public Messages<SysMainRecords> detail(
			@RequestParam(name="recordId",required=true) Long recordId){
		SysMainRecords record = mainRecordsService.findById(recordId);
		return Messages.success(record);
	}
	/**
	 * @param res
	 * @return 导出所有数据
	 **/
	@GetMapping(value="/export_excel")
    public void exportExcel(HttpServletResponse res){
		List<SysMainRecords> all=mainRecordsService.findAll();
		List<SysMainRecordsExcelVo> voList=new ArrayList<>();
		if(all != null){
			for(SysMainRecords records:all){
				SysMainRecordsExcelVo vo=new SysMainRecordsExcelVo();
				vo.setCarNo(records.getCarNo());
				vo.setContent(records.getContent());
				vo.setCreateTime(DateUtil.format(records.getCreateTime(),"yyyy-MM-dd HH:mm:ss"));
				vo.setDriverName(records.getDriverName());
				vo.setMainNo(records.getMainNo());
				vo.setMainPro(records.getMainPro());
				vo.setSiteName(records.getSiteName());
				voList.add(vo);
			}
		}
		String title = "保养记录.xlsx";
        String[] rowName={"保养记录编号","提交时间","所属工地","所属车辆设备","保养项目","保养内容","提交者"};
        List<Object> datalist=new ArrayList<>(1);
        datalist.addAll(voList);
        ExportExcelUtils.exportExcel(res,title,rowName,datalist);
	}
}